<?php
/*********************************************************************************
 * Copyright (C) 2011-2013 X2Engine Inc. All Rights Reserved.
 *
 * X2Engine Inc.
 * P.O. Box 66752
 * Scotts Valley, California 95067 USA
 *
 * Company website: http://www.x2engine.com
 * Community and support website: http://www.x2community.com
 *
 * X2Engine Inc. grants you a perpetual, non-exclusive, non-transferable license
 * to install and use this Software for your internal business purposes.
 * You shall not modify, distribute, license or sublicense the Software.
 * Title, ownership, and all intellectual property rights in the Software belong
 * exclusively to X2Engine.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT.
 ********************************************************************************/

/**
 * Widget class for displaying all available inline actions.
 *
 * Displays tabs for "log a call","new action" and the like.
 *
 * @package X2CRM.components
 */
class Publisher extends X2Widget {
	public $associationType;		// type of record to associate actions with
	public $associationId = '';		// record to associate actions with
	public $assignedTo = null;	// user actions will be assigned to by default

	// show all tabs by default
	public $showLogACall = true;
	public $showNewAction = true;
	public $showNewComment = true;
	public $showNewEvent = false;
	public $halfWidth = false;
    public $showQuickNote = true;

	public function run() {
		$model = new Actions;
		$model->associationType = $this->associationType;
		$model->associationId = $this->associationId;
		if($this->assignedTo)
			$model->assignedTo = $this->assignedTo;
		else
			$model->assignedTo = Yii::app()->user->getName();

        Yii::app()->clientScript->registerScript('loadEmails',"
            function loadFrame(id,type){
                if(type!='Action' && type!='QuotePrint'){
                    var frame='<iframe style=\"width:99%;height:99%\" src=\"".(Yii::app()->controller->createUrl('/actions/viewEmail/'))."?id='+id+'\"></iframe>';
                }else if(type=='Action'){
                    var frame='<iframe style=\"width:99%;height:99%\" src=\"".(Yii::app()->controller->createUrl('/actions/viewAction/'))."?id='+id+'&publisher=true\"></iframe>';
                } else if(type=='QuotePrint'){
		    var frame='<iframe style=\"width:99%;height:99%\" src=\"".(Yii::app()->controller->createUrl('/quotes/print'))."?id='+id+'\"></iframe>';

		}
                if(typeof x2ViewEmailDialog != 'undefined') {
                    if($(x2ViewEmailDialog).is(':hidden')){
                        $(x2ViewEmailDialog).remove();

                    }else{
                        return;
                    }
                }

                x2ViewEmailDialog = $('<div></div>', {id: 'x2-view-email-dialog'});

                x2ViewEmailDialog.dialog({
                    title: '".Yii::t('app','View history item') /* Changed to generic title from "View" +type because there's no practical way to translate javascript variables */ ."',
                    autoOpen: false,
                    resizable: true,
                    width: '650px',
                    show: 'fade'
                });
                jQuery('body')
                    .bind('click', function(e) {
                        if(jQuery('#x2-view-email-dialog').dialog('isOpen')
                            && !jQuery(e.target).is('.ui-dialog, a')
                            && !jQuery(e.target).closest('.ui-dialog').length
                        ) {
                            jQuery('#x2-view-email-dialog').dialog('close');
                        }
                    });

                x2ViewEmailDialog.data('inactive', true);
                if(x2ViewEmailDialog.data('inactive')) {
                    x2ViewEmailDialog.append(frame);
                    x2ViewEmailDialog.dialog('open').height('400px');
					x2ViewEmailDialog.data('inactive', false);
                } else {
                    x2ViewEmailDialog.dialog('open');
                }
            }
            $(document).on('ready',function(){
                var t;
                $(document).on('mouseenter','.email-frame',function(){
                    var id=$(this).attr('id');
                    t=setTimeout(function(){loadFrame(id,'Email')},500);
                });
                $(document).on('mouseleave','.email-frame',function(){
                    clearTimeout(t);
                });
                $('.quote-frame').mouseenter(function(){
                    var id=$(this).attr('id');
                    t=setTimeout(function(){loadFrame(id,'Quote')},500);
                }).mouseleave(function(){
                    clearTimeout(t);
                }); // Legacy quote pop-out view
		$('.quote-print-frame').mouseenter(function(){
			var id=$(this).attr('id');
			t=setTimeout(function(){loadFrame(id,'QuotePrint')},500);
		}).mouseleave(function(){
			clearTimeout(t);
		}); // New quote pop-out view
            });
        ",CClientScript::POS_HEAD);

		$this->render($this->halfWidth? 'publisherHalfWidth':'publisher',
			array(
				'model' => $model,
				'showLogACall'=>$this->showLogACall,
				'showNewAction'=>$this->showNewAction,
				'showNewComment'=>$this->showNewComment,
				'showNewEvent'=>$this->showNewEvent,
                'showQuickNote'=>$this->showQuickNote,
			)
		);
	}
}
